package uploader;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UploaderServlet extends HttpServlet{
	public void doPost(HttpServletRequest req, HttpServletResponse res) 
	throws ServletException, IOException { 
		res.setContentType("text/plain");
		// Accept the new message as the "message" parameter 
		String message = req.getParameter("message");
		String[] components = message.split("SEPARATORFORSERVLET");
		//if(components!=null) {
		String fileName1 = components[0];
		String fileName2 = components[2];
		String log1 = components[1];
		String log2 = components[3];
		// Broadcast it to all listening clients 
		FileWriter fileWriter1;
		FileWriter fileWriter2;
		//if (message != null) {
			fileWriter1 = new FileWriter(getServletContext().getRealPath("/") 
					+ File.separator + "WEB-INF" + File.separator + "Logs" + File.separator+ fileName1);
			fileWriter2 = new FileWriter(getServletContext().getRealPath("/") 
					+ File.separator + "WEB-INF" + File.separator + "Logs" + File.separator+ fileName2);
			fileWriter1.write(log1);
			fileWriter1.close();
			fileWriter2.write(log2);
			fileWriter2.close();
//		FileWriter fileWriter;
//		if (message != null) {
//			fileWriter = new FileWriter(getServletContext().getRealPath("/") 
//					+ File.separator + "WEB-INF" + File.separator + "my_file.txt");
//			fileWriter.write(message);
//			fileWriter.close();
//		}
		//}
		//}
		// Set the status code to indicate there will be no response 
		res.setStatus(HttpServletResponse.SC_NO_CONTENT); 
	}
}
